<?php
namespace app\center\controller;
use app\center\logic\LoginLogic;

/**
 * 登录api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/16
 * Time: 16:51
 */
class Login extends Base {

    public function __construct(){
        parent::__construct();
        $this->l_logic = new LoginLogic();
    }

    /**
     * 登录控制台
     * @return \think\response\Json
     */
    public function ajax_login_admin(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查登录控制台规则
        $r = $this->l_logic->check_login_admin_rule($u_detail['openid'],$params['code']);
        if(!$r['code']){
            return json($r);
        }
        $alc_detail = $r['data']['alc_detail'];

        $maps = [];
        $maps[] = ['id','=',$alc_detail['id']];

        $alc_data = [];
        $alc_data['openid'] = $u_detail['openid'];
        $alc_data['l_time'] = date('Y-m-d H:i:s');
        $r = model('admin_login_code')->force()->save($alc_data,$maps);
        if($r !== false){
            return json($this->l_logic->get_response(1,'登录成功'));
        }
        return json($this->l_logic->get_response(0,'登录失败'));
    }
}